
define(['apps/utils/tools','apps/utils/dashboard'],function (tools,dashboard){
  var domId = "content",tableId = "tableId",dashModule = [],dimArr=[],datas = {};
  
  function qypj(){};
  function ylnl(){}
  function yyzl(){}
  function fznl(){}
  function zwfx(){}
  /**
   * 负债经营行性
   */
  function fzjy(){
  
  }
  /**
   * 支付能力
   */
  function payAbility(){
  
  }
  function dqchnl(){
  
  	
  }
  function cash_flow(){
  
  }
  
  
  function dupont(){}
  function capital_chain(){}

  function debtBefore(){
  	     $("#myTab>li[class=active]").attr("load",1);
         $('#myTab a').click(function (e) {
		   e.preventDefault();
		   $(this).tab('show');
		    var index = $(this).parent().index();
		      initDash(dashModule[index]);
		 });
		 
		 
  }
  function debt(id){}
  function assets(id){
    // alert("profita---"+id);
  }
  function profita(id){
    // alert("profita---"+id);
  }
  
  function queryIndustry(activeModule,keyArr){
     var params = tools.getDimParams(dimArr);
     params["code"] = keyArr.join(",");//async:false, 
     function success(json){
       // activeModule["industry"]  = json;
        tools.applyIf(activeModule["industry"],json);
     };
     var url = tools.getBasePathUrl() +"dash/industry.cnbi";
      tools.ajax({url : url,dataType :'json',async:false,data :params,success:success});
  }
  function initIndustry(activeModule){
     	var industry  = activeModule["industry"];
        if(!industry)return;
        
  	    if(!activeModule["inds"])activeModule["inds"] = industry;
  	    industry = activeModule["inds"];
        var keyArr=[];
     	for(var key in industry){
     		var arr = industry[key],arr1  = key.split("_");
     		
     		var ss = arr1[0];
     	   if(arr != null && arr instanceof Array && arr.length > 0){//说明是自己特定的
     	  	var obj={sname:arr1[2],id:arr1[1]};
	     	    for(var i=0,len = arr.length;i<len;i++){
	     	    	  obj[arr[i]["id"]] = arr[i]["scode"]-0;
	     	    }
	     	    industry[ss] = obj;
     		}else{//去数据库去查啦
     			if(arr == null)keyArr.push(key);
     		}
     		delete industry.key;
     	}
     	if(keyArr.length > 0){
     	    queryIndustry(activeModule,keyArr);
     	}else{
     	   activeModule["industry"] = industry;
     	}
}
  function initData(activeModule){
  	if(activeModule["dataType"] == "sql"){
	     var params = tools.getDimParams(dimArr);
	     params["season"] = activeModule.id;//async:false, 
	     tools.mask();
	     function success(json){
	     	datas = json;
	     	if(activeModule["factType"]){
	     	  if(activeModule["factType"] instanceof String){
	     	     activeModule["factType"] = activeModule["factType"].split(",");
	     	  }
	     	   if(activeModule["factType"].length >1){
	     	       activeModule["datas"]  = json[activeModule["factType"][0]];
	     	       //tools.unmask();
	     	       return ;
	     	   }
	     	}
	     	activeModule["datas"]  = json;
	     	//tools.unmask();
	     };
	     var url = tools.getBasePathUrl() +"dash/datas.cnbi";
	      tools.ajax({url : url,dataType :'json',async:false,data :params,success:success});
  	}
  }
  function initDash(activeModule){
         if(activeModule.load && activeModule.load == 1){
         	  dashboard.updateMoule(activeModule);
         }else{
		     activeModule["updataInputAfter"] = eval(activeModule["id"]);
		     initIndustry(activeModule);
		     initData(activeModule);
			 dashboard.init(activeModule);
			 activeModule["load"] = 1;
			
         
         }
  }
  /**
   * 模型数据更新功能实现
   *  $("#myTab>li[class=active]").attr("load",1);
   */
  function query(params){
       tools.console(params);
       var module = dashModule;
       if(dashModule instanceof Array){
       	  var index = $("#myTab>li[class=active]").index();
       	  module = dashModule[index];
       }
       initIndustry(module);
	   initData(module);
	   dashboard.updateDatas(module["datas"],module["industry"]);
  }
 /* function mouseOverAndIn(){
     $("#jgxzjmId").mouseover(function(){}).mouseout(function(){});
  }*/
  function chartClick(){
     $("#chartTable>div").click(function(){
           var domId = $(this).attr("id"),isShow = $(this).attr("isShow"),
            caption = $("#"+domId).attrCnbiSoft("caption");
         if(isShow == "1"){//说明已经单击了，再次单击的话自己缩小来 要显示3个亲兄弟
         	 $("#"+domId).attrCnbiSoft({showlabels: 0});
         	 $("#"+domId).attrCnbiSoft({showlegend: 0}); 
            tools.console("缩小图的单击"+caption+"-------显示了3个亲兄弟");
            $(this).attr("style","");
            $(this).siblings().fadeToggle("slow","linear");
            $(this).attr("isShow","0").attr("title","单击放大【"+caption+"】");
         }else{//说明这是要放大图的单击， 自己放大还要隐藏了3个亲兄弟
         	var width = $(this).parent().width()-40;
             $("#"+domId).attrCnbiSoft({showlabels: 1});
             $("#"+domId).attrCnbiSoft({showlegend: 1}); 
             tools.console("放大图的单击"+caption+"----隐藏了3个亲兄弟");
             $(this).css({"width":width}).siblings().fadeToggle("slow","linear");
             $(this).attr("isShow","1").attr("title","单击缩小【"+caption+"】并显示其它图形");
         }
    });
  }
  function factChange(){
    $("a[fact=1]").click(function(){
        if(!$(this).hasClass(tools.cls)){
        	 var scode = $(this).attr("scode");
             dashboard.updateDatas(datas[scode]);
             tools.setSelect(this);
             tools.removeSelect($(this).siblings())
		}
    });
  }
  function dash(module){
	     if(module instanceof Array){
    	      debtBefore();
    	      initDash(module[0]);
    	 }else{
    	 	if(module["id"] == "capital_chain"){
    	 	  chartClick();
    	 	}else if(module["id"] == "profita"){
    	 	   factChange();
    	 	}
    	     initDash(module);
    	 }
  }
  
  function success(msg){
		tools.levelNav(Utils.bean);
		$("#"+domId).empty().append(msg);
		tools.viewChange();
		tools.dimComponentClick(Utils.dimMapJson,query);
		//exportBind();
		dimArr = tools.getDimArrByMap(Utils.dimMapJson);//dblclick
		tools.bindPanelHeaderDbClick();
		dashModule = module;
		dash(module);
		tools.unmask();
		
		
   }
  /** 
   * 初始方法
   */
  function init (){
  	  tools.eventBind({domId:domId,tableId:tableId,success:success});
   }
  return {
    init: init
  }
});